package com.hxht.cmp.shiro;

import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.NamedFilterList;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author alice on 2017/7/25 0025.
 * @version 1.0
 * @since 1.0
 */
public class ShiroFilerChainManager {
    @Autowired
    private DefaultFilterChainManager filterChainManager;
    private Map<String, NamedFilterList> defaultFilterChains;

    @PostConstruct
    public void init() {
        defaultFilterChains = new HashMap<>(filterChainManager.getFilterChains());
    }

    public void initFilterChains(List urlFilters) {
        //1、首先删除以前老的filter chain并注册默认的
        filterChainManager.getFilterChains()
                .clear();
        if (defaultFilterChains != null) {
            filterChainManager.getFilterChains().putAll(defaultFilterChains);
        }
        //2、循环URL Filter 注册filter chain
//        for (UrlFilter urlFilter : urlFilters) {
//            String url = urlFilter.getUrl();
//            //注册roles filter
//            if (!StringUtils.isEmpty(urlFilter.getRoles())) {
//                filterChainManager.addToChain(url, "roles", urlFilter.getRoles());
//            }
//            //注册perms filter
//            if (!StringUtils.isEmpty(urlFilter.getPermissions())) {
//                filterChainManager.addToChain(url, "perms", urlFilter.getPermissions());
//            }
//        }
    }
}
